package com.ttc.ssm.service.home.impl;

import com.ttc.ssm.common.DeliveryPage;
import com.ttc.ssm.common.MailPage;
import com.ttc.ssm.common.ResumePage;
import com.ttc.ssm.controller.common;
import com.ttc.ssm.mapper.home.*;
import com.ttc.ssm.po.Company;
import com.ttc.ssm.po.Customer;
import com.ttc.ssm.po.Delivery;
import com.ttc.ssm.po.Invite;
import com.ttc.ssm.po.InviteActivity;
import com.ttc.ssm.po.*;
import com.ttc.ssm.service.home.HomeCustomerService;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by Jiarui on 2016/8/3.
 */
public class HomeCustomerServiceImpl implements HomeCustomerService {

    @Autowired
    HomeCustomerMapper homeCustomerMapper;

    @Autowired
    HomeCompanyMapper homeCompanyMapper;

    @Autowired
    HomeDeliveryMapper homeDeliveryMapper;

    @Autowired
    HomeInviteMapper homeInviteMapper;

    @Autowired
    HomeInviteActivityMapper homeInviteActivityMapper;

    @Autowired
    HomeDeliveryActivityMapper homeDeliveryActivityMapper;

    @Override
    public Customer getCustomerByTelephone(String telephone) throws Exception {
        return homeCustomerMapper.selectByTelephone(telephone);
    }

    @Override
    public Customer getCustomerById(int customer_id) throws Exception {
        return homeCustomerMapper.selectById(customer_id);
    }

    @Override
    public Company getCompanyByEmail(String e_mail) throws Exception {
        return homeCompanyMapper.selectByEmail(e_mail);
    }

    @Override
    public int register_customer(Customer customer, HttpServletRequest request) throws Exception {
        Date now = new Date();
        customer.setDate_added(now);
        customer.setDate_modified(now);
        customer.setCustomer_group_id(1);
        customer.setStatus(1);
        customer.setSalt(common.getSalt());
        String password = common.SHA1(customer.getSalt()+common.SHA1(customer.getSalt()+common.SHA1(customer.getPassword())));
        customer.setPassword(password);
        customer.setIp(common.getIpAddr(request));
        homeCustomerMapper.register_customer(customer);
        return customer.getCustomer_id();
    }

    @Override
    public int checkExist(Map map) throws Exception {
        return homeCustomerMapper.checkExist(map);
    }

    @Override
    public int update_customer(Customer customer, HttpServletRequest request) throws Exception {
        customer.setDate_modified(new Date());
        customer.setIp(common.getIpAddr(request));
        homeCustomerMapper.update_customer(customer);
        return customer.getCustomer_id();
    }

    @Override
    public int save_psd(Customer customer) throws Exception {
        customer.setDate_modified(new Date());
        String password = common.SHA1(customer.getSalt()+common.SHA1(customer.getSalt()+common.SHA1(customer.getPassword())));
        customer.setPassword(password);
        homeCustomerMapper.save_psd(customer);
        return customer.getCustomer_id();
    }

    @Override
    public int countDeliveryRowsByMap(Map map) throws Exception {
        return homeDeliveryMapper.countRowsByCustomerId(map);
    }

    @Override
    public int countInviteRowsByCustomerId(int customer_id) throws Exception {
        return homeInviteMapper.countRowsByCustomerId(customer_id);
    }

    @Override
    public List<Delivery> selectDeliveryForPage(DeliveryPage resumePage) throws Exception {
        return homeDeliveryMapper.selectDeliveryForPage(resumePage);
    }

    @Override
    public int countRows(DeliveryPage resumePage) throws Exception {
        return homeDeliveryMapper.countRows(resumePage);
    }

    @Override
    public List<Invite> selectInviteForPage(DeliveryPage deliveryPage) throws Exception {
        return homeInviteMapper.selectInviteForPage(deliveryPage);
    }

    @Override
    public int countInviteRows(DeliveryPage deliveryPage) throws Exception {
        return homeInviteMapper.countInviteRows(deliveryPage);
    }

    @Override
    public int countRowsNoReadCustomer(int customer_id) throws Exception {
        return homeInviteActivityMapper.countRowsNoReadCustomer(customer_id);
    }

    @Override
    public int countRowsNoReadCustomerForEmail(MailPage mailPage) throws Exception {
        return homeInviteActivityMapper.countRowsNoReadCustomerForMail(mailPage);
    }

    @Override
    public int countRowsNoReadCustomerDeliveryForEmail(MailPage mailPage) throws Exception {
        return homeDeliveryActivityMapper.countRowsNoReadCustomerForEmail(mailPage);
    }

    @Override
    public List<InviteActivity> selectActivityCustomer(int customer_id) throws Exception {
        return homeInviteActivityMapper.selectActivityCustomer(customer_id);
    }

    @Override
    public List<InviteActivity> selectActivityCustomerForPage(MailPage mailPage) throws Exception {
        return homeInviteActivityMapper.selectActivityCustomerForPage(mailPage);
    }

    @Override
    public int countRowsNoReadCustomerDelivery(int customer_id) throws Exception {
        return homeDeliveryActivityMapper.countRowsNoReadCustomer(customer_id);
    }

    @Override
    public List<DeliveryActivity> selectDeliveryActivityCustomer(int customer_id) throws Exception {
        return homeDeliveryActivityMapper.selectActivityCustomer(customer_id);
    }

    @Override
    public List<DeliveryActivity> selectDeliveryActivityCustomerForMail(MailPage mailPage) throws Exception {
        return homeDeliveryActivityMapper.selectActivityCustomerForPage(mailPage);
    }
}
